package com.software.levelupandroid;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.software.levelupandroid.entity.User;
import com.software.levelupandroid.home.HomeActivity;
import com.software.levelupandroid.loginAndRegistration.ForgetActivity;
import com.software.levelupandroid.loginAndRegistration.IView;
import com.software.levelupandroid.loginAndRegistration.LoginActivity;
import com.software.levelupandroid.loginAndRegistration.RegisterActivity;
import com.software.levelupandroid.loginAndRegistration.presenter.IPresenter;
import com.software.levelupandroid.loginAndRegistration.presenter.MainPresenter;
import com.software.levelupandroid.utils.StringUtil;
import com.software.levelupandroid.utils.ToastUtil;

public class MainActivity extends AppCompatActivity implements IView {
    private Button btnMainLogin;
    private Button btnMainRegister;
    private ImageButton ibLoginClose;
    private ImageView ivMainLogo, ivMainAppName, ivWelcome;
    private LinearLayout llMainBtn;
    private LinearLayout llMainLoginEmail;
    private ImageView ivMainLoginEmailLogo;
    private EditText edtMainLoginEmail;
    private LinearLayout llMainLoginPwd;
    private ImageView ivMainLoginPwdLogo;
    private EditText edtMainLoginPwd;
    private TextView tvMainLoginForgetPwd;
    private Button btnMainLoginLogin;
    private IPresenter mainPresenter;
    private ProgressDialog progressDialog;

    private Button btnForgetPad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainPresenter = new MainPresenter(this, this);

        initProgressDialog();

        initView();

        setListener();
    }

    private void initProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在登录，请稍等……");
    }

    /**
     * 设置监听器
     */
    private void setListener() {
        //设置登录按钮的监听器
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showVisibility(false);
                showSheetDialog();
            }
        });

        //注册按钮的监听器
        btnMainRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


    }

    /**
     * 展开弹窗
     */
    @SuppressLint("MissingInflatedId")
    private void showSheetDialog() {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this, R.style.MainBottomSheetDialog);
        View bottomView = LayoutInflater.from(this).inflate(R.layout.login_dialog, null);
        bottomSheetDialog.setContentView(bottomView);

        //控件绑定
        bindView(bottomView);

        FrameLayout bottomSheet = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
        BottomSheetBehavior<FrameLayout> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        bottomSheetBehavior.setSkipCollapsed(true);

        bottomSheetDialog.show();

        bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED){
                    showVisibility(false);
                }else {
                    showVisibility(true);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

        //设置按钮的点击监听事件，关闭弹窗
        ibLoginClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetDialog.dismiss();
                showVisibility(true);
            }
        });

        //设置对email输入框的焦点监听器
        edtMainLoginEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    Drawable emailFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.baseline_email_focus_24);
                    Drawable backgroundFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.background_focus);
                    if (emailFocus != null && backgroundFocus != null){
                        ivMainLoginEmailLogo.setImageDrawable(emailFocus);
                        llMainLoginEmail.setBackground(backgroundFocus);
                    }else {
                        Log.e("FocusChange", "Drawable resources not found for focus state");
                    }
                }else {
                    Drawable emailUnFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.baseline_email_unfocus_24);
                    Drawable backgroundUnFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.background_unfocus);
                    if (emailUnFocus != null && backgroundUnFocus != null){
                        ivMainLoginEmailLogo.setImageDrawable(emailUnFocus);
                        llMainLoginEmail.setBackground(backgroundUnFocus);
                    }else {
                        Log.e("FocusChange", "Drawable resources not found for unfocus state");
                    }
                }
            }
        });

        //设置对pwd输入框的焦点监听器
        edtMainLoginPwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    Drawable emailFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.baseline_lock_focus_24);
                    Drawable backgroundFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.background_focus);

                    Drawable emailUnFocus0 = ContextCompat.getDrawable(MainActivity.this, R.drawable.baseline_email_unfocus_24);
                    Drawable backgroundUnFocus0 = ContextCompat.getDrawable(MainActivity.this, R.drawable.background_unfocus);
                    ivMainLoginEmailLogo.setImageDrawable(emailUnFocus0);
                    llMainLoginEmail.setBackground(backgroundUnFocus0);

                    ivMainLoginPwdLogo.setImageDrawable(emailFocus);
                    llMainLoginPwd.setBackground(backgroundFocus);
                }else {
                    Drawable emailUnFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.baseline_lock_unfocus_24);
                    Drawable backgroundUnFocus = ContextCompat.getDrawable(MainActivity.this, R.drawable.background_unfocus);

                    ivMainLoginPwdLogo.setImageDrawable(emailUnFocus);
                    llMainLoginPwd.setBackground(backgroundUnFocus);
                }
            }
        });

        //设置登录点击事件
        btnMainLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = edtMainLoginEmail.getText().toString();
                String password = edtMainLoginPwd.getText().toString();
                if (StringUtil.isEmpty(email) || StringUtil.isEmpty(password)){
                    ToastUtil.showToastOnUI(MainActivity.this, "用户名和密码不能为空");
                }
                mainPresenter.login(email, password);
            }
        });

        //设置忘记密码按钮的点击事件
        btnForgetPad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ForgetActivity.class);
                startActivity(intent);
            }
        });

    }

    //淡入动画
    private void fadeIn(View view){
        view.setVisibility(View.VISIBLE);
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
        fadeIn.setDuration(500);
        fadeIn.start();
    }

    //淡出动画
    private void fadeOut(View view){
        ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
        fadeOut.setDuration(500);
        fadeOut.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(@NonNull Animator animator) {
                //动画开始时调用
            }

            @Override
            public void onAnimationEnd(@NonNull Animator animator) {
                //动画结束时调用，将视图设置为GONE或INVISIBLE
                view.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationCancel(@NonNull Animator animator) {
                //动画取消时调用
            }

            @Override
            public void onAnimationRepeat(@NonNull Animator animator) {
                //动画重复时调用
            }
        });
        fadeOut.start();
    }

    /**
     * 控件是否显示
     * @param isVisible
     */
    private void showVisibility(boolean isVisible){
        if (isVisible){
            fadeIn(ivMainLogo);
            fadeIn(ivMainAppName);
            fadeIn(ivWelcome);
            fadeIn(llMainBtn);
        }else {
            fadeOut(ivMainLogo);
            fadeOut(ivMainAppName);
            fadeOut(ivWelcome);
            fadeOut(llMainBtn);
        }
    }

    /**
     * 弹窗控件绑定
     * @param bottomView
     */
    private void bindView(View bottomView) {
        ibLoginClose = bottomView.findViewById(R.id.ib_login_close);
        llMainLoginEmail = bottomView.findViewById(R.id.ll_mainLogin_email);
        ivMainLoginEmailLogo = bottomView.findViewById(R.id.iv_mainLogin_emailLogo);
        edtMainLoginEmail = bottomView.findViewById(R.id.edt_mainLogin_email);
        llMainLoginPwd = bottomView.findViewById(R.id.ll_mainLogin_pwd);
        ivMainLoginPwdLogo = bottomView.findViewById(R.id.iv_mainLogin_pwdLogo);
        edtMainLoginPwd = bottomView.findViewById(R.id.edt_mainLogin_pwd);
        tvMainLoginForgetPwd = bottomView.findViewById(R.id.tv_mainLogin_forgetPwd);
        btnMainLoginLogin = bottomView.findViewById(R.id.btn_mainLogin_login);
        btnForgetPad = bottomView.findViewById(R.id.tv_mainLogin_forgetPwd);
    }

    /**
     * 控件绑定
     */
    private void initView() {
        ivMainLogo = findViewById(R.id.iv_main_logo);
        ivMainAppName = findViewById(R.id.iv_main_appName);
        ivWelcome = findViewById(R.id.iv_welcome);
        llMainBtn = findViewById(R.id.ll_main_btn);
        btnMainLogin = findViewById(R.id.btn_main_login);
        btnMainRegister = findViewById(R.id.btn_main_register);

    }

    @Override
    public void showProgress() {
        progressDialog.show();
    }

    @Override
    public void hideProgress() {
        progressDialog.dismiss();
    }

    @Override
    public void loginSuccess(User user) {
        Intent intent = new Intent(MainActivity.this, HomeActivity.class);
        startActivity(intent);
    }

    @Override
    public void loginFailure(String msg) {
        ToastUtil.showToastOnUI(MainActivity.this, msg);
    }
}